AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
--include('entities/base_wire_entity/init.lua')
include( 'shared.lua' )
util.PrecacheSound( "SB/Charging.wav" )

function ENT:Initialize()

	self.Entity:SetModel( "models/Slyfo/mcpcannon.mdl" ) 
	self.Entity:SetName("Medium Cannon")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	if WireAddon then
		self.Inputs = WireLib.CreateInputs( self, { "Fire" } )
	end
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	
	--self.val1 = 0
	--RD_AddResource(self.Entity, "Munitions", 0)
	
	self.CDL = {}
	self.CDL[1] = 0
	self.CDL[2] = 0
	self.CDL[3] = 0
	self.CDL[4] = 0
	self.CDL["1r"] = true
	self.CDL["2r"] = true
	self.CDL["3r"] = true
	self.CDL["4r"] = true
	self.Entity:SetNetworkedInt("Shots",4)


end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "weapon_MedCannon" )
	ent:SetPos( SpawnPos + Vector( 0,0,50 ) )
	ent:Spawn()
	ent:Activate()
	ent.SPL = ply
	
	return ent
	
end

function ENT:TriggerInput(iname, value)		
	if (iname == "Fire") then
		if (value > 0) then
			self.Entity:HPFire()
		end
	end
end

function ENT:PhysicsUpdate()

end

function ENT:Think()
	local MCount = 0
	for n = 1, 4 do
		if (CurTime() >= self.CDL[n]) then
			if self.CDL[n.."r"] == false then
				self.CDL[n.."r"] = true
				self.Entity:EmitSound("Buttons.snd26")
			end
			MCount = MCount + 1
		end
	end
	self.Entity:SetShots(MCount)
end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end

function ENT:HPFire()
	if (CurTime() >= self.MCDown) then
		for n = 1, 4 do
			if (CurTime() >= self.CDL[n]) then
				self.Entity:FFire(n)
				return
			end
		end
	end
end

function ENT:FFire( CCD )
	local ammo = self:GetResourceAmount("Ammo Exp")
	if (ammo >= 50) then
		local CVel = self.Entity:GetPhysicsObject():GetVelocity():Length()
		self:ConsumeResource("Ammo Exp", 50)
		local Size = "ammo_shell_med"
		local Pos = self.Entity:GetPos() + (self.Entity:GetUp() * 10) + (self.Entity:GetForward() * (160 + CVel))
		local Ang = self.Entity:GetForward():Angle() 
		local Vel = self.Entity:GetForward() * 1000
		self:MakeShell(CCD,Size,Pos,Ang,Vel)
		self:Recoil(1000)
		self.CDL[CCD] = CurTime() + 9
		self.CDL[CCD.."r"] = false
		self.Entity:EmitSound("Buttons.snd24")
		self.MCDown = CurTime() + 0.5
		local effectdata = EffectData()
		effectdata:SetOrigin(self.Entity:GetPos() +  self.Entity:GetForward() * 150)
		effectdata:SetStart(self.Entity:GetPos() +  self.Entity:GetForward() * 150)
		util.Effect( "Explosion", effectdata )
	end
end
